package RMIClient.gui;

import RMIClient.EventController;
import RMIClient.UserRMIDTO;

import java.util.List;
import java.util.Observer;

import javax.swing.JFrame;

import eventReservationServer.EventRMIDTO;
import eventReservationServer.loginGateway.ValidationException;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.JScrollPane;

/**
 * CHECK THE GUI SOURCE CODE PROVIDED AS A REFERENCE FEEL FREE TO ADD OR
 * ORGANIZE THE GUI AS YOU PLEASE KEEP IT SIMPLE
 * 
 */

public class RMIClientGUI extends JFrame implements Observer
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private JTextField textField;
	private JPasswordField passwordField;
	private JTextField textField_1;
	private JTextField textField_2;
	private JTextField textField_3;

	private EventController eventController = null;

	private JTable table;
	private DefaultTableModel dtm = null;

	public RMIClientGUI(EventController event)
	{
		this.eventController = event;

		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(10, -12, 801, 496);

		getContentPane().setLayout(null);

		JLabel lblUsername = new JLabel("Username: ");
		lblUsername.setBounds(53, 51, 82, 16);
		getContentPane().add(lblUsername);

		textField = new JTextField();
		textField.setBounds(136, 45, 134, 28);
		getContentPane().add(textField);
		textField.setColumns(10);

		JLabel lblPassword = new JLabel("Password: ");
		lblPassword.setBounds(282, 51, 76, 16);
		getContentPane().add(lblPassword);

		passwordField = new JPasswordField();
		passwordField.setBounds(353, 45, 103, 28);
		getContentPane().add(passwordField);

		JButton btnLogin = new JButton("Login");
		btnLogin.addActionListener(new ActionListener()
		{
			@SuppressWarnings("deprecation")
			public void actionPerformed(ActionEvent arg0)
			{
				UserRMIDTO user = null;
				
				try
				{
					user = eventController.login(textField.getText(), passwordField.getText());
					textField_1.setText(user.getName());
					textField_2.setText(user.getSex());
					textField_3.setText(String.valueOf(user.getAge()));
				} 
				catch (ValidationException e)
				{
					JOptionPane.showMessageDialog(RMIClientGUI.this, e.getMessage());
				}
			}
		});
		btnLogin.setBounds(499, 46, 117, 29);
		getContentPane().add(btnLogin);

		JLabel lblName = new JLabel("Name: ");
		lblName.setBounds(41, 143, 61, 16);
		getContentPane().add(lblName);

		textField_1 = new JTextField();
		textField_1.setEnabled(false);
		textField_1.setBounds(114, 137, 134, 28);
		getContentPane().add(textField_1);
		textField_1.setColumns(10);

		JLabel lblSex = new JLabel("Sex: ");
		lblSex.setBounds(282, 143, 61, 16);
		getContentPane().add(lblSex);

		textField_2 = new JTextField();
		textField_2.setEnabled(false);
		textField_2.setBounds(322, 137, 134, 28);
		getContentPane().add(textField_2);
		textField_2.setColumns(10);

		JLabel lblAge = new JLabel("Age: ");
		lblAge.setBounds(488, 143, 61, 16);
		getContentPane().add(lblAge);

		textField_3 = new JTextField();
		textField_3.setEnabled(false);
		textField_3.setBounds(524, 137, 134, 28);
		getContentPane().add(textField_3);
		textField_3.setColumns(10);

		JButton btnGetevents = new JButton("GetEvents");
		btnGetevents.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent arg0)
			{
				List<EventRMIDTO> listEventRMIDTO = eventController.getEvents();
				String[] columnNames = {"EventServer", "EventName", "Description", "Date", "Places"};
				
				dtm = new DefaultTableModel(columnNames, listEventRMIDTO.size());
				
				for (int i = 0; i < listEventRMIDTO.size(); i++)
				{
					dtm.setValueAt(listEventRMIDTO.get(i).getEventServer(), i, 0);
					dtm.setValueAt(listEventRMIDTO.get(i).getName(), i, 1);
					dtm.setValueAt(listEventRMIDTO.get(i).getDescription(), i, 2);
					dtm.setValueAt(listEventRMIDTO.get(i).getDate(), i, 3);
					dtm.setValueAt(listEventRMIDTO.get(i).getAvailablePlaces(), i, 4);
				}

				table.setModel(dtm);
			}
		});
		btnGetevents.setBounds(72, 361, 117, 29);
		getContentPane().add(btnGetevents);

		JButton btnReserveplace = new JButton("ReservePlace");
		btnReserveplace.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				//System.out.println("EventServer: " + dtm.getValueAt(table.getSelectedRow(), 0).toString() + " y EventName: " + dtm.getValueAt(table.getSelectedRow(), 1));
				RMIClientGUI.this.eventController.reservePlace(dtm.getValueAt(table.getSelectedRow(), 0).toString(), dtm.getValueAt(table.getSelectedRow(), 1).toString());
			}
		});
		btnReserveplace.setBounds(282, 361, 117, 29);
		getContentPane().add(btnReserveplace);

		JButton btnExit = new JButton("Exit");
		btnExit.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				// Notificamos al controller que dejamos de ser observer
				eventController.deleteLocalObserver(RMIClientGUI.this);
				System.exit(0);
			}
		});
		btnExit.setBounds(519, 361, 117, 29);
		getContentPane().add(btnExit);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(26, 177, 675, 161);
		getContentPane().add(scrollPane);
		
		table = new JTable();
		scrollPane.setViewportView(table);

		// Para mantener un enlace con el GUI (por medio del Local Observer)
		eventController.addLocalObserver(this);
		setVisible(true);
	}

	public void update(java.util.Observable o, Object arg)
	{
		EventRMIDTO eventRMIDTO = (EventRMIDTO) arg;
		
		int row = 0;
		boolean encontrado = false;
		
		while ((row < table.getModel().getColumnCount()) && (encontrado == false))
		{
			if (table.getModel().getValueAt(row, 1).equals(eventRMIDTO.getName())) 
			{
				if (table.getModel().getValueAt(row, 0).equals(eventRMIDTO.getEventServer()))
				{
					table.getModel().setValueAt(eventRMIDTO.getAvailablePlaces(), row, 4);
					encontrado = true;
				}	
			}
			row++;
		}

	}
}
